﻿var app = angular.module('app', ['angular.jquery']);
var item = null;
var flag = false;
var url = Editor.Url;
var pageId = 2;
var pageSize = 10;
var objCurrent = null;
var isBool = true;
app.directive('jqdatepicker', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            element.datepicker({
                dateFormat: 'dd-mm-yy',
                onSelect: function (date) {
                    scope.nv.nGAYSINH = date;
                    scope.$apply();
                }
            });
        }
    };
});

app.directive('demongay', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            element.datepicker({
                dateFormat: 'dd-mm-yy',
                onSelect: function (date) {
                    scope.nv.nGAYSINH = date;
                    scope.$apply();
                }
            });
        }
    };
});

app.controller('DemoCtrl', [
    '$scope', '$http',
    'AppDialogService',
function ($scope, $http, AppDialogService) {
    'use strict';
    //var url = Editor.Url + "TD/GetAll";
    $http.get(url + "/nv/GetAll")
     .success(function (response) {
         $scope.orderByField = 'tENNV';
         $scope.reverseSort = true;
         $scope.nhanviens = response;
     });

    var params = {
        "pageId": pageId,
        "pageSize": pageSize
    };
    /*
    $http.post(url + "/nv/Get", params).success(function (response) {        
        console.dir(response);
        $scope.nhanviens = response.items;
    });
    */
    objCurrent = Editor.getItem("nv");
    $scope.showAdd = function () {
        if (objCurrent != null) {
            return objCurrent.isadd;
        }
        return isBool;
    }
    $scope.showEdit = function () {
        if (objCurrent != null) {
            return objCurrent.edit;
        }
        return isBool;
    }
    $scope.showDelete = function () {
        if (objCurrent != null) {
            return objCurrent.isdelete;
        }
        return isBool;
    }
    $http.get(url + "/TD/GetAll")
    .success(function (response) {
        $scope.trinhdos = response;        
    });

    $http.get(url + "/tinh/GetAll")
    .success(function (response) {
        $scope.tinhs = response;
    });

    $http.get(url + "/quan/GetAll")
    .success(function (response) {
        $scope.quans = response;
    });

    $http.get(url + "/vu/GetVuDetails")
    .success(function (response) {
        $scope.vus = response;
    });

    $scope.loadData = function () {
        $http.get(url + "/nv/GetAll")
                .success(function (response) {
                    $scope.orderByField = 'tENNV';
                    $scope.reverseSort = true;
                    $scope.nhanviens = response;
                });
    }

    $scope.delete = function (id) {
        var a = confirm("Bạn có thực sự muốn xóa dòng này?");
        if (a === false)
            return;
        $http.delete(url + "/api/NhanVienAPI/" + id).success(function (data, status, headers) {
            $scope.loadData();
        });
    }

    $scope.detail = function (_item) {        
        Editor.Save(_item, "detail");
        var url = Editor.Url + "/nv/Detail";
        location.href = url;
    }

    $scope.appDialog = function (_flag, _item) {
        item = _item;
        flag = _flag;
        AppDialogService.openDialog({
            testValue: 'application'
        }).then(function (result) {
            if (result.ok) {
                if (flag) {
                    console.dir(result.obj);
                    $http.put(url + "/api/NhanVienAPI/" + result.obj.iD, result.obj).success(function (data, status, headers) {
                        if (status === 200) {
                            alert("cập nhập thành công");
                        }
                    });
                }
                else {
                    $http.post(Editor.Url + "/api/NhanVienAPI", result.obj).success(function (data, status, headers) {
                        $scope.loadData();
                    });
                }
            }
        });

    };

}]);
app.directive('datepicker', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        link: function (scope, element, attrs, ngModelCtrl) {
            element.datepicker();
            element.bind('blur keyup change', function(){
                var model = attrs.ngModel;
                if (model.indexOf(".") > -1) scope[model.replace(/\.[^.]*/, "")][model.replace(/[^.]*\./, "")] = element.val();
                else scope[model] = element.val();
            });
        }
    };
});

app.controller('DialogCtrl', [
    '$scope',
    'AppDialogService',
function ($scope, AppDialogService) {
    'use strict';

    $scope.nv = null;

    $scope.onOpen = function () {        
        $scope.nv = item;
        $scope.value = new Date();
        if (item != null){
            try {
                var datetime = new Date($scope.nv.nGAYSINH);                
                //$scope.value = datetime;// new Date(datetime.getYear(), datetime.getMonth(), datetime.getDate());
                $("#ngay").val(datetime.getDate());
                $("#thang").val(datetime.getMonth() + 1);
                $("#nam").val(datetime.getFullYear());
            }
            catch (ex) {
                console.dir(ex);
            }
        } 
    };

    $scope.$parent.onOk = function () {
        //$scope.nv.nGAYSINH = $scope.value;
        var year = parseInt($("#nam").val(),10);
        var month = parseInt($("#thang").val(), 10);
        var day = parseInt($("#ngay").val(), 10);
        $scope.nv.nGAYSINH = new Date(year, month, day);
        AppDialogService.closeDialog({ ok: true, obj: $scope.nv });
    };

    $scope.onCancel = function () {
        AppDialogService.closeDialog({ ok: false });
    };

    $scope.beforeClose = function () {
        return "";
    };

}]);